<?php
namespace App\Models\Admin;

use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
* @comment 供应商表
*
* @property int $id
* @property string $business_number 商户号
* @property string $phone 手机号
* @property string $company_name 公司名称
* @property string $actual_name 真实姓名
* @property string $business_license_number 营业执照号码
* @property float $balance 余额
* @property float $platform_fees 平台费用
* @property int $platform_fee_expiration_time 平台费用过期时间
* @property string $id_number 身份证号
* @property string $id_photo 身份证照片 必须本人手持身份证上半身(姓名,证件号码必须完全清晰)
* @property string $id_card_front_photo 身份证正面照
* @property string $id_card_reverse_photo 身份证反面照
* @property int $account_verification 账号验证:-1=刚注册,未提交认证,0=刚提交,1=验证通过,2=验证失败,被拒绝
* @property string $login_password 登录密码
* @property string $business_license 营业执照
* @property string $register_time 注册时间
* @property string $verification_time 实名认证操作时间: 提交|审核
*
* @property SupplierConfiguration|null $config
*/
class Supplier extends Authenticatable implements JWTSubject
{
    use Notifiable;

    /**
     * @var string
     */
    protected $connection = 'admin';
    /**
     * @var string
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $table = 'supplier';

    /**
     * @var string[]
     */
    protected $fillable = ['id', 'business_number', 'phone', 'company_name', 'actual_name', 'business_license_number', 'balance', 'platform_fees', 'platform_fee_expiration_time', 'id_number', 'id_photo', 'id_card_front_photo', 'id_card_reverse_photo', 'account_verification', 'login_password', 'business_license', 'register_time', 'verification_pass_time'];

    /**
     * @return mixed
     */
    public function getJWTIdentifier(): mixed
    {
        return $this->getKey();
    }

    /**
     * @return array
     */
    public function getJWTCustomClaims(): array
    {
        return [];
    }

    /**
     * @return string
     */
    function getAuthPassword(): string
    {
        return $this->login_password;
    }

    /**
     * @return HasMany
     */
    public function sms(): HasMany
    {
        return $this->hasMany(SupplierSms::class, 'sid', 'id');
    }

    /**
     * @return SupplierSms|Model|null
     */
    public function getLastNoUseSms(): SupplierSms|Model|null
    {
        return $this->sms()->where('state', 2)->orderBy('id')->first();
    }

    /**
     * @return HasMany
     */
    public function authRecord(): HasMany
    {
        return $this->hasMany(SupplierAuthenticationAuditRecord::class, 'sid', 'id');
    }

    /**
     * @return string
     */
    public static function businessNumberCreate(): string
    {
        return '1'.substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * @return HasMany
     */
    public function account(): HasMany
    {
        return $this->hasMany(PlatformFeeRechargeRecord::class, 'sid', 'id');
    }

    /**
     * @return HasOne
     */
    public function config(): HasOne
    {
        return $this->hasOne(SupplierConfiguration::class, 'sid', 'id');
    }

    /**
     * @return string
     */
    public function getDbConnectKey(): string
    {
        return dbConnectKey($this->id);
    }

    /**
     * @return Connection
     */
    public function connection(): Connection
    {
        return DB::connection($this->getDbConnectKey());
    }

    /**
     * @param SupplierConfiguration|null $config
     * @return bool|string
     */
    public function registerDbConnect(?SupplierConfiguration $config = null): bool|string
    {
        $dbKey = $this->getDbConnectKey();
        // 检测db是否注册
        if (checkDbConnectIsRegister($dbKey)) {
            return $dbKey;
        }
        $config = $config ?? $this->config;
        if (!$config) {
            return false;
        }
        return $config->registerDbConfig($dbKey);
    }
    /**
     * @return HasOne
     */
    public function smsConfig(): HasOne
    {
        return $this->hasOne(SupplierSmsConfig::class, 'sid', 'id');
    }
}
